﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.DataAccess.IRepositories;
using System.ComponentModel.Composition;
using System.Collections;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(IFileRepository))]
    public class FileRepository : IFileRepository
    {
        private Connection conn;
        public FileRepository()
        {
            conn = new Connection();
        }

        public Int64 SaveFile(File file)
        {
            using (MXHEntities context = conn.GetContext())
            {
                if (file.FileID > 0)
                {
                    context.Files.Attach(new File { FileID = file.FileID });
                    context.Files.ApplyCurrentValues(file);
                }
                else
                {
                    file.CreateDate = DateTime.Now;
                    context.Files.AddObject(file);
                }
                context.SaveChanges();
            }
            return file.FileID;
        }


        public List<File> GetFilesByFolderID(List<Guid> folderIds)
        {
            List<File> result = new List<File>();
            using (MXHEntities context = conn.GetContext())
            {
                //folderIds.ForEach(n => result.Add(context.Files.FirstOrDefault(i => i.DefaultFolderID == n)));
                foreach(Guid folder in folderIds)
                {
                    var item = from fol in context.Files
                               where fol.DefaultFolderID == folder
                               select fol;
                    if (item.Count() > 0)
                        result.Add(item.FirstOrDefault());
                }
            }
            //result.RemoveAll(n => n == null);
            result = result.OrderByDescending(n => n.CreateDate).ThenByDescending(n => n.CreateDate.TimeOfDay).ToList();
            return result;
        }


        public File GetFileByFileID(long FileID)
        {
            File result;
            using (MXHEntities context = conn.GetContext())
            {
                result = context.Files.SingleOrDefault(n => n.FileID == FileID);
            }
            return result;
        }


        public File GetFileByAccountID(int accountID)
        {
            File result = new File();
            using (MXHEntities context = conn.GetContext())
            {
                var files = from a in context.Files
                            where a.AccountID == accountID
                            orderby a.FileID descending
                            select a;
                try
                {
                    if (files != null)
                        result = files.First();
                }
                catch (Exception e) { result = null; }
            }
            return result;
        }

        public void UpdateIsPublicResourceByFileName(string fileName)
        {
            using (MXHEntities context = conn.GetContext())
            {
                var item = from fi in context.Files
                           where fi.FileName == fileName
                           select fi;
                if (item.Count() > 0)
                {
                    File fi = item.ToList().Last();
                    fi.IsPublicResource = true;
                    context.SaveChanges();
                }
            }
        }
    }
}
